package cn.edu.swu.lzr.Operator;

import jakarta.servlet.annotation.WebServlet;

@WebServlet("/deleteBook")
public class Pager {
  private String Url;
  private int Total;

    public String getUrl() {
        return Url;
    }

    public int getCurrentPage() {
        return CurrentPage;
    }

    public void setCurrentPage(int currentPage) {
        CurrentPage = currentPage;
    }

    public int getPageSize() {
        return PageSize;
    }

    public void setPageSize(int pageSize) {
        this.PageSize = pageSize;
    }

    public int getTotal() {
        return Total;
    }

    public void setTotal(int total) {
        this.Total = total;
    }

    public void setUrl(String url) {
        this.Url = url;
    }

    private int PageSize;
  private int CurrentPage;

    public Pager(){}

    public Pager(int total,int pageSize,int currentPage,String url){
        this.PageSize = pageSize;
        this.CurrentPage = currentPage;
        this.Total = total;
        this.Url = url;
    }

    public String toHtml(){
        StringBuilder sb = new StringBuilder();
        sb.append("<div>");
        sb.append(String.format("当前%s页/共%s页，&nbsp;&nbsp;",this.getCurrentPage(),this.getCurrentPage()));
        sb.append(this.getCurrentPage()>1?
                String.format("<a href ='%s‘page = %s&pageSize='%s'>前一页</a>",
                this.getUrl(),this.getCurrentPage()-1,this.getPageSize()) : ""
        );
        for(int i=1, t = this.getTotalPage(); i<=t; i++) {
            sb.append(String.format("&nbsp;&nbsp;<a href='%spage=%s&pageSize=%s'>&nbsp;%s&nbsp;</a>",
                    this.getUrl(), i, this.getPageSize() , i));
        }
        sb.append(this.getCurrentPage() < this.getTotalPage() ?
                String.format("&nbsp;&nbsp;<a href='%spage=%s&pageSize=%s'>后一页</a>",
                        this.getUrl(), this.getCurrentPage()+1, this.getPageSize()) : "");
        sb.append("</div>");
        return sb.toString();
    }

    public int getTotalPage(){
        return Double.valueOf(Math.ceil(this.getTotal() * 1.0 / this.getPageSize())).intValue();
    }
}
